% Appendix Section A7 Quantifying Arrow Securities
% this file computes the Arrow Securities to replicate GLTHI and GHHW contracts 

clear;
addpath functions
close all
load('./results/model_parameters_main.mat');

nInd = model_parameters.nInd;
CAPH = model_parameters.CAPH;
MU = model_parameters.MU;
rho = model_parameters.rho;
gamma = model_parameters.gamma;
T =70;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
% german contract
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            

load('./results/allstates_main.mat')
Pspot_N = simulation_output.Pspot_N;

% someone who starts at 1
bx = adsecurity_gr(1,Pspot_N,MU,CAPH,T,rho);
check = Pspot_N(1,1) - MU(1,1,1) - CAPH(1,:,1)*bx*rho

% some who starts at 1 and stays at 1

bx1 = adsecurity_gr([1 1],Pspot_N,MU,CAPH,T,rho);

% this individual does not lapse
% therefore is paying Pspot_N(1,1); 
% and is receiving the payment from the security bx(1)

check = Pspot_N(1,1)-MU(1,1,2)-CAPH(1,:,2)*bx1*rho + bx(1)

% reported in appendix
bglthi = [bx';bx1'];
writetable(array2table(bglthi),'./results/bglthi.xlsx');
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
% GHHW contract (for Ed 13)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            

%income in thousands

Y1 = simulation_output.Wio_abi;

% GHHW consumption guarantees

Cg1 = simulation_output.Cg_abi;
Nstates = size(Cg1,1)-1;
bx_hhw=adsecurity_hhw(1,Cg1,MU,Y1,CAPH,T,rho,Nstates);

check = Cg1(1,1) - (Y1(1)- MU(1,1,1) - CAPH(1,:,1)*bx_hhw*rho)

bx1_hhw=adsecurity_hhw([1 1],Cg1,MU,Y1,CAPH,T,rho,Nstates);

% this individual lapses; therefore is not getting payment from bx_hhw

check = Cg1(1,2) - (Y1(2)- MU(1,1,2) - CAPH(1,:,2)*bx1_hhw*rho)

% reported in appendix

bghhw = [bx_hhw';bx1_hhw'];
writetable(array2table(bghhw),'./results/bghhw.xlsx');

         